﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using OA.Model;
using OA.Web.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using X.PagedList;
using Z.EntityFramework.Plus;

namespace OA.Web.Areas.Member.Controllers
{
    public class NoteController : BaseMemberController
    {

        private IMemoryCache _cache;
        private ICookie _cookie;
        public NoteController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
            _cookie = cookie;
        }

        public IActionResult Index(int type=1)
        {
            return View();
        }

        /// <summary>
        /// 日程提醒
        /// </summary>
        /// <returns></returns>
        public IActionResult GetData(int page = 1, int limit = 10)
        {
            string dt = DateTime.Now.ToString("yyyy-MM-dd");
            var info = this.dbContextFactory.DbContext.Note.Where(m => m.EmpID == EmpID && m.ReminderDate.CompareTo(dt) >= 0).OrderBy(m => m.ReminderDate).ToPagedList(page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = info.TotalItemCount,
                data = info.ToList()
            };
            return Json(result);
        }


        public IActionResult GetData2(int page = 1, int limit = 10)
        {
            var info = this.dbContextFactory.DbContext.Note.Where(m => m.EmpID == EmpID).OrderByDescending(m=>m.ID).ToPagedList(page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = info.TotalItemCount,
                data = info.ToList()
            };
            return Json(result);
        }


        public IActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Add(Note model)
        {
            BaseResult baseResult = new BaseResult();
            try
            {
                List<Note> addList = new List<Note>();
                for (int i = 0; i < model.Days; i++)
                {
                    Note item = new Note();
                    item.EmpID = Info.EmpID;
                    item.CreateDateTime = DateTime.Now;
                    item.ReminderDate = DateTime.Parse(model.ReminderDate).AddDays(i).ToString("yyyy-MM-dd");
                    item.Content = model.Content;
                    item.Status = 1;
                    addList.Add(item);
                }
                this.dbContextFactory.DbContext.Note.AddRange(addList);
                this.dbContextFactory.DbContext.SaveChanges();
            }
            catch(Exception ex)
            {
                baseResult.State = 400;
                baseResult.Msg = ex.Message.ToString();
            }
            return Json(baseResult);
        }

        [HttpPost]
        public IActionResult Delete(int id)
        {
            BaseResult baseResult = new BaseResult();
            this.dbContextFactory.DbContext.Note.Where(m => m.ID == id && m.EmpID == EmpID).Delete();            
            return Json(baseResult);
        }
        

    }
}